//
//  HWExchangeView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/18.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWExchangeView.h"
#import "HWExchangeButton.h"

@interface HWExchangeView()
@property (nonatomic, strong)UILabel *moneyLabel;//余额
@property (nonatomic, strong)NSMutableArray *moneyBtnArr;//
@property (nonatomic, copy)NSArray *moneyArr;//

@property (nonatomic, assign)NSInteger numOfMoney;
 
@property (nonatomic, strong)NSString *moneyStr;//钱
@end

@implementation HWExchangeView

- (NSMutableArray *)moneyBtnArr
{
    if (!_moneyBtnArr)
    {
        _moneyBtnArr = [[NSMutableArray alloc]init];
    }
    return _moneyBtnArr;
}

- (NSArray *)moneyArr
{
    if (!_moneyArr)
    {
        _moneyArr = @[@{@"money":@"10",@"time":@"1000"},
                      @{@"money":@"20",@"time":@"2000"},
                      @{@"money":@"50",@"time":@"5000"},
                      @{@"money":@"100",@"time":@"10000"}];
    }
    return _moneyArr;
}

- (UILabel *)moneyLabel
{
    if (!_moneyLabel)
    {
        _moneyLabel = [HWUtilties creatLabelWithFrame:CGRectMake(0,18*kUIScaleBasePlus,SCREEN_WIDTH,17*kUIScaleBasePlus)
                                         textAligment:NSTextAlignmentCenter
                                             textClor:LCHexColor(0x000000)
                                             textFont:15*kUIScaleBasePlus];
    }
    return _moneyLabel;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.moneyLabel];
        [self creatButton];
        [self attention];
    }
    return self;
}

- (void)creatButton
{
    UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(18*kUIScaleBasePlus,CGRectGetMaxY(self.moneyLabel.frame)+23*kUIScaleBasePlus,120*kUIScaleBasePlus,12*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentLeft
                                                 textClor:LCHexColor(0x828282)
                                                 textFont:12*kUIScaleBasePlus];
    titleLabel.text = @"快速兑换";
    [self addSubview:titleLabel];
    
    CGFloat width = 160.0*kUIScaleBasePlus;
    CGFloat height = 80.0*kUIScaleBasePlus;
    
    CGFloat margin = 15*kUIScaleBasePlus;
    
    for (int i=0; i<4; i++)
    {
        HWExchangeButton *button = [[HWExchangeButton alloc]initWithFrame:CGRectMake(margin+(i%2)*(margin+width),(i/2)*(margin+height)+CGRectGetMaxY(titleLabel.frame)+17*kUIScaleBasePlus,width,height)];
        [button getRechargeDate:self.moneyArr[i]];
        button.tag = i;
        if (i == 0)
        {
            [button changeColor:0];
            
        }
        [self.moneyBtnArr addObject:button];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    
    UIButton *sureBtn = [HWUtilties creatButtonWithFrame:CGRectMake(19*kUIScaleBasePlus,294*kUIScaleBasePlus,337*kUIScaleBasePlus,40*kUIScaleBasePlus)
                                            textAligment:NSTextAlignmentCenter
                                                    text:@"确认兑换"
                                                textClor:LCHexColor(0xFFFFFF)
                                               buttonBGC:LCHexColor(0xF54337)
                                                textFont:19*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(sure)];
    [self addSubview:sureBtn];
    
    
}

#pragma mark -Method
- (void)sure
{
    NSString *monryStr = self.moneyArr[self.numOfMoney][@"time"];
    if ([self.moneyStr integerValue]<monryStr.integerValue)
    {
        [SVProgressHUD showErrorWithStatus:@"金币不足"];
        return;
    }
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"bookcoin":self.moneyStr
                               };
    
    WeakSelf(self)
    [MyInfoManager getChangeMoney:prarmers sucess:^(id  _Nonnull acceptData) {
        
        
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"兑换现金失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
}

#pragma mark - 选中兑换金额
- (void)btnClick:(UIButton *)sender
{
    self.numOfMoney = sender.tag;
    
    [self.moneyBtnArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HWExchangeButton *button = obj;
        if (idx == sender.tag)
        {
            [button changeColor:0];
        }
        else
        {
            [button changeColor:1];
        }
    }];
}

#pragma mark - 温馨提示
- (void)attention
{
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(16*kUIScaleBasePlus,363*kUIScaleBasePlus,3*kUIScaleBasePlus,15*kUIScaleBasePlus)];
    lineView.backgroundColor = LCHexColor(0xF54337);
    [self addSubview:lineView];
    
    UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(lineView.frame)+10*kUIScaleBasePlus,363*kUIScaleBasePlus, 120*kUIScaleBasePlus,14*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentLeft
                                                 textClor:LCHexColor(0x333333)
                                                 textFont:15*kUIScaleBasePlus];
    titleLabel.text = @"温馨提示";
    [titleLabel setFont:[UIFont boldSystemFontOfSize:15*kUIScaleBasePlus]];
    [self addSubview:titleLabel];
    
    UILabel *contextLabel = [HWUtilties creatLabelWithFrame:CGRectMake(16*kUIScaleBasePlus,CGRectGetMaxY(titleLabel.frame)+19*kUIScaleBasePlus,344*kUIScaleBasePlus,91*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0x595656)
                                                   textFont:13*kUIScaleBasePlus];
    contextLabel.numberOfLines = 0;
    contextLabel.text = @"1.申请提交后，系统会自动进行兑换，用户可在个人中心中查看\n2.如有问题，可关注公众号【嗨星球】咨询。";
    [contextLabel sizeToFit];
    [self addSubview:contextLabel];
}

#pragma mark - 获取数据
- (void)getDate:(NSString *)money
{
    NSString *str = [NSString stringWithFormat:@"金币余额：%@金币",money];
    self.moneyStr = money;
    self.moneyLabel.attributedText = [money addAttriButedString:str
                                                          Color:LCHexColor(0xF54337)
                                                           Font:[UIFont systemFontOfSize:18*kUIScaleBasePlus]
                                                           rang:NSMakeRange(5,str.length-5)];
}

@end
